#include<bits/stdc++.h>
using namespace std;
int main()
{
	return 0;
}
//  反转对
    //  注意升序和降序  根据单调性，优化处理方式
class Solution
{
public:
    int tmp[500005];
    int reversePairs(vector<int>& nums)
    {
        return mergeSort(nums, 0, nums.size() - 1);
    }
    int mergeSort(vector<int>& nums, int left, int right)
    {
        if (left >= right)
            return 0;
        int mid = (left + right) >> 1;
        int ans = 0;
        ans += mergeSort(nums, left, mid);
        ans += mergeSort(nums, mid + 1, right);

        int cur1 = left, cur2 = mid + 1;
        while (cur1 <= mid) //  降序
        {
            while (cur2 <= right && nums[cur1] / 2.0 <= nums[cur2])
                cur2++;
            if (cur2 > right)
                break;
            ans += right - cur2 + 1;
            cur1++;
        }

        cur1 = left, cur2 = mid + 1;
        int i = left;
        while (cur1 <= mid && cur2 <= right)
        {
            tmp[i++] = nums[cur1] >= nums[cur2] ? nums[cur1++] : nums[cur2++];
        }
        while (cur1 <= mid)
            tmp[i++] = nums[cur1++];
        while (cur2 <= right)
            tmp[i++] = nums[cur2++];
        for (int j = left; j <= right; j++)
        {
            nums[j] = tmp[j];
        }
        return ans;
    }
};



// 反转对   降序  
class Solution
{
public:
    int tmp[500005];
    int reversePairs(vector<int>& nums)
    {
        return mergeSort(nums, 0, nums.size() - 1);
    }
    int mergeSort(vector<int>& nums, int left, int right)
    {
        if (left >= right)
            return 0;
        int mid = (left + right) >> 1;
        int ans = 0;
        ans += mergeSort(nums, left, mid);
        ans += mergeSort(nums, mid + 1, right);

        int cur1 = left, cur2 = mid + 1;
        while (cur2 <= right)   //  升序排列
        {
            while (cur1 <= mid && nums[cur1] / 2.0 <= nums[cur2])
                cur1++;
            if (cur1 > mid)
                break;
            ans += mid - cur1 + 1;
            cur2++;
        }

        cur1 = left, cur2 = mid + 1;
        int i = left;
        while (cur1 <= mid && cur2 <= right)
        {
            tmp[i++] = nums[cur1] <= nums[cur2] ? nums[cur1++] : nums[cur2++];
        }
        while (cur1 <= mid)
            tmp[i++] = nums[cur1++];
        while (cur2 <= right)
            tmp[i++] = nums[cur2++];
        for (int j = left; j <= right; j++)
        {
            nums[j] = tmp[j];
        }
        return ans;
    }
};